﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Threading;
using System.Net;
using System.Windows;


namespace RealtimeStockReader
{
    [Serializable]
    public class StockQuoteOld : INotifyPropertyChanged
    {
        private static string chartDomain = @"http://ichart.yahoo.com/t?s=";

        private static string yahooAddress = @"http://download.finance.yahoo.com/d/quotes.csv?s=";
        private static string requestedFields = @"&f=snd1t1l1baohgpcc1va2mwerr1qdyj1t5l9c6p4b1a3x";
        //not implemented yet
        private Decimal alertUnder = 0.0m;
        private Decimal alertOver = 0.0m;
        private bool alertedUnder = false;
        private bool alertedOver = false;

        private int amount;
        private Decimal buyValue;
        private Decimal personalWinPercent;
        private Decimal personalWinMoney;

        private string symbol;
        private string fullName;
        private Decimal bid, ask;
        private Decimal open, dayHigh, dayLow;
        private Decimal prevClose;
        private string changeAndPercent;
        private int volume;
        private int averageDailyVolume;
        private string dayRange;
        private string yearRange;
        private Decimal earningsPerShare;
        private Decimal peRatio;
        private DateTime dividbedPayDate;
        private DateTime exDevidedDate;
        private Decimal dividendPerShare;
        private Decimal dividendYield;
        private Decimal marketCapitalization;

        private string lastTradeDate;
        private string lastTradeTime;
        private Decimal lastTradeRate;
        private Decimal change;
        private Decimal percent;
        private string stockexchange;


        private bool downwards;

        public StockQuoteOld(string symbol)
        {
            this.Symbol = symbol.ToUpper();
        }

        public StockQuoteOld(string symbol, int amount, Decimal pricePerShare)
        {
            this.Symbol = symbol.ToUpper();
            this.amount = amount;
            this.buyValue = pricePerShare;
        }

        public StockQuoteOld(string symbol, int amount, Decimal pricePerShare, decimal alertUnd, decimal alertov)
        {
            this.Symbol = symbol.ToUpper();
            this.amount = amount;
            this.buyValue = pricePerShare;
            this.alertOver = alertov;
            this.alertUnder = alertUnd;
        }

        public override string ToString()
        {
            return Symbol + "  ---  " + LastTradeRate.ToString("f2") + "  ---  "
                + Change.ToString("f2") + "  ---  "
                + Percent.ToString("f2") + "%";
        }

        #region Properties


        public Decimal AlertUnder
        {
            get { return alertUnder; }
            set { alertUnder = value; }
        }
        public Decimal AlertOver
        {
            get { return alertOver; }
            set { alertOver = value; }
        }
        public Decimal BuyValue
        {
            get { return buyValue; }
            set { buyValue = value; }
        }

        public int Amount
        {
            get { return amount; }
            set { amount = value; }
        }

        public Decimal PersonalWinMoney
        {
            get { return this.personalWinMoney; }
            set
            {
                this.personalWinMoney = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("PersonalWinMoney"));
            }
        }
        public Decimal PersonalWinPercent
        {
            get { return this.personalWinPercent; }
            set
            {
                this.personalWinPercent = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("PersonalWinPercent"));
            }
        }
        public string ChartDomain
        {
            get { return chartDomain + this.Symbol + "&" + DateTime.Now.Ticks.ToString(); }
        }
        public string Symbol
        {
            get { return this.symbol; }
            set
            {
                this.symbol = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Symbol"));
            }
        }
        public string StockExchange
        {
            get { return this.stockexchange; }
            set
            {
                this.stockexchange = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("StockExchange"));
            }
        }
        public string FullName
        {
            get { return this.fullName; }
            set
            {
                this.fullName = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("FullName"));
            }
        }
        public string YearRange
        {
            get { return this.yearRange; }
            set
            {
                this.yearRange = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("YearRange"));
            }
        }
        public string DayRange
        {
            get { return this.dayRange; }
            set
            {
                this.dayRange = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("DayRange"));
            }
        }
        public Decimal Change
        {
            get { return this.change; }
            set
            {
                change = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Change"));
            }
        }
        public Decimal LastTradeRate
        {
            get { return this.lastTradeRate; }
            set
            {
                this.lastTradeRate = value;

                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("LastTradeRate"));
            }
        }
        public Decimal Percent
        {
            get { return this.percent; }
            set
            {
                percent = value;

                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Percent"));
            }
        }
        public Decimal Bid
        {
            get { return this.bid; }
            set
            {
                bid = value;

                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Bid"));
            }
        }
        public Decimal Ask
        {
            get { return this.ask; }
            set
            {
                ask = value;

                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Ask"));
            }
        }
        public Decimal Open
        {
            get { return this.open; }
            set
            {
                open = value;

                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Open"));
            }
        }
        public Decimal DayHigh
        {
            get { return this.dayHigh; }
            set
            {
                dayHigh = value;

                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("DayHigh"));
            }
        }
        public Decimal DayLow
        {
            get { return this.dayLow; }
            set
            {
                dayLow = value;

                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("DayLow"));
            }
        }
        public Decimal PrevClose
        {
            get { return this.prevClose; }
            set
            {
                prevClose = value;

                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("PrevClose"));
            }
        }
        public int Volume
        {
            get { return this.volume; }
            set
            {
                volume = value;

                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Volume"));
            }
        }
        public bool Downwards
        {
            get { return this.downwards; }
            set
            {
                downwards = value;

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Downwards"));
                }
            }
        }
        public string LastTradeTime
        {
            get { return this.lastTradeTime; }
            set
            {
                lastTradeTime = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("LastTradeTime"));

            }
        }
        public string LastTradeDate
        {
            get { return this.lastTradeDate; }
            set
            {
                lastTradeDate = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("LastTradeDate"));

            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public void UpdateStockQuote()
        {
            string url = yahooAddress + Symbol + requestedFields;
            string htmlText = GetHTML(url);
            htmlText.Replace("\"", "");
            string[] data = htmlText.Split(',');

            FullName = data[1];

            LastTradeDate = data[2].Substring(1, data[2].Length - 2);
            LastTradeTime = data[3].Substring(1, data[3].Length - 2);

            Bid = ParseDecimal(data[5]);
            Ask = ParseDecimal(data[6]);

            Open = ParseDecimal(data[7]);
            DayHigh = ParseDecimal(data[8]);
            DayLow = ParseDecimal(data[9]);
            PrevClose = ParseDecimal(data[10]);

            changeAndPercent = data[11];
            Change = ParseDecimal(data[12]);

            try
            {
                Volume = int.Parse(data[13]);
            }
            catch (Exception)
            {
                Volume = 0;
            }

            averageDailyVolume = int.Parse(data[14]);

            DayRange = data[15].Substring(1, data[15].Length - 2);
            YearRange = data[16].Substring(1, data[16].Length - 2);

            earningsPerShare = ParseDecimal(data[17]);
            peRatio = ParseDecimal(data[18]);

            dividbedPayDate = ParseDateTime(data[19]);
            exDevidedDate = ParseDateTime(data[20]);

            dividendPerShare = ParseDecimal(data[21]);
            dividendYield = ParseDecimal(data[22]);
            marketCapitalization = ParseDecimal(data[23]);


            LastTradeRate = ParseDecimal(data[25]);

            Downwards = (data[27].Substring(1, 1) == "-");

            Percent = ParseDecimal(data[27].Substring(2, data[27].Length - 4)); //remove % and +/- char

            if (amount > 0 && buyValue > 0)
            {
                PersonalWinMoney = Decimal.Round((LastTradeRate - buyValue) * amount, 2);
                PersonalWinPercent = Decimal.Round((100 * (amount * LastTradeRate) / (amount * buyValue)) - 100, 2);
            }
            StockExchange = data[30].Substring(1, data[30].Length - 3);
            if (alertOver != 0.0m && !alertedOver && LastTradeRate > alertOver)
            {
                alertedOver = true;


            }
            else if (alertUnder != 0.0m && !alertedUnder && LastTradeRate < alertUnder)
            {
                alertedUnder = true;

            }
        }

        private Decimal ParseDecimal(string value)
        {
            Decimal retVal;
            Decimal.TryParse(value.Replace('.', ','), out retVal);
            return Decimal.Round(retVal, 2);
        }
        private DateTime ParseDateTime(string value)
        {
            DateTime temp;
            DateTime.TryParse(value.Substring(1, value.Length - 2), out temp);
            return temp;
        }

        public string GetHTML(string url)
        {
            WebClient wc = new WebClient();
            string htmlString = "";

            try
            {
                htmlString = wc.DownloadString(url);
            }
            catch (Exception) { }
            finally
            {
                wc.Dispose();
            }
            return htmlString;
        }

        public void RemoveEvents()
        {
            PropertyChanged = null;
        }
    }
}


